﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bricks.MiuiThemeMaker.OrmModel;

namespace Bricks.MiuiThemeMaker.BRMiuiThemeMaker
{
    /*********************************************************
     * 1.版权：积木工场 Bricks Factory
     * 2.作用：用户BLL
     * 3.创建人：罗泽新
     * 4.创建日期：2012-04-23
     * 5.最后修改人：罗泽新
     * 6.最后修改日期：2012.04.05
     * 7.修改项目：
     * *******************************************************/
    public class UserBLL
    {
        /// <summary>
        /// 验证用户（登录）
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="dbType">数据库类型</param>
        /// <returns></returns>
        public bool CheckUser(BF_User user, DBType dbType)
        {
            bool result = false;
            switch (dbType)
            {
                //Sql Server
                case DBType.MsSql: break;

                //Sql Compact
                case DBType.SqlCompact:
                    using (DBEntities dbContext = new DBEntities())
                    {
                        var isExist = dbContext.BF_User.Where(v => v.Username == user.Username && v.Password == user.Password).FirstOrDefault();
                        if (isExist != null)
                            result = true;
                    }
                    break;

                default: break;
            }
            return result;
        }

        /// <summary>
        /// 编辑用户（含新增）
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="dbType">数据库类型</param>
        public void EditUser(BF_User user, DBType dbType)
        {
            switch (dbType)
            {
                //Sql Server
                case DBType.MsSql: break;

                //Sql Compact
                case DBType.SqlCompact:
                    using (DBEntities dbContext = new DBEntities())
                    {
                        //新增
                        if (user.ID == Guid.Empty)
                        {
                            dbContext.BF_User.AddObject(user);
                        }
                        //编辑
                        else
                        {
                            dbContext.BF_User.Attach(user);
                            dbContext.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
                        }
                        dbContext.SaveChanges();
                    }
                    break;

                default: break;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="dbType">数据库类型</param>
        public void DeleteUser(BF_User user, DBType dbType)
        {
            switch (dbType)
            {
                //Sql Server
                case DBType.MsSql: break;

                //Sql Compact
                case DBType.SqlCompact:
                    using (DBEntities dbContext = new DBEntities())
                    {
                        var record = dbContext.BF_User.Where(v => v.ID == user.ID).FirstOrDefault();
                        if (record != null)
                        {
                            dbContext.BF_User.DeleteObject(record);
                            dbContext.SaveChanges();
                        }
                    }
                    break;

                default: break;
            }
        }
    }
}
